package main

import (
	"bufio"
	"fmt"
	"io"
	"net"
	"path/filepath"
	"strings"
	"sync"
)

// 服务器里面有根目录和锁
type FTPServer struct {
	rootDir string
	mu      sync.RWMutex
}

// 创建一个新的FTP服务器实例， NewFTPServer
func NewFTPServer(rootDir string) *FTPServer {
	//创建绝对路径？
	absPath, _ := filepath.Abs(rootDir)
	return &FTPServer{
		rootDir: absPath,
	}
}

func (s *FTPServer) handleConnection(conn net.Conn) {
	defer conn.Close()

	//拿到ftp服务器的绝对路径, 并创建读取器
	currentDir := s.rootDir
	reader := bufio.NewReader(conn)
	writer := bufio.NewWriter(conn)

	writer.WriteString("220 weclcome")
	writer.Flush()

	for {
		command, err := reader.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				fmt.Println("Client disconnected")
			} else {
				fmt.Printf("Read error: %v\n", err)
			}
			return
		}

		//去掉换行符和回车符
		command = strings.TrimSpace(command)
		parts := strings.Fields(command)
		if len(parts) == 0 {
			continue
		}

		cmd := strings.ToUpper(parts[0])
		args := parts[1:]
		fmt.Printf("Received command: %s %v\n", cmd, args)

		switch cmd {
		case "CD":
			if len(args) != 1 {
				writer.WriteString("")
			}
		}

	}
}

func main() {

}
